package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.utils.RedisKey;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@DubboService
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper contentMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //查询全部广告
    @Override
    public List<TbContent> getContentList() {
        return contentMapper.selectByExample(null);
    }

    //增加广告
    @Override
    public void addContent(TbContent content) {
        stringRedisTemplate.delete(RedisKey.BANNER);
        stringRedisTemplate.delete(RedisKey.DAYPUSH);
        stringRedisTemplate.delete(RedisKey.YOULIKE);
        contentMapper.insertSelective(content);
    }

    //回显广告信息
    @Override
    public TbContent editContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    //修改广告
    @Override
    public void updateContent(TbContent content) {
        stringRedisTemplate.delete(RedisKey.BANNER);
        stringRedisTemplate.delete(RedisKey.DAYPUSH);
        stringRedisTemplate.delete(RedisKey.YOULIKE);
        contentMapper.updateByPrimaryKeySelective(content);
    }

    //删除广告
    @Override
    public void deleteContent(Long[] ids) {
        stringRedisTemplate.delete(RedisKey.BANNER);
        stringRedisTemplate.delete(RedisKey.DAYPUSH);
        stringRedisTemplate.delete(RedisKey.YOULIKE);
        List<Long> list = Arrays.asList(ids);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        contentMapper.deleteByExample(example);
    }

    //获取轮播图广告
    @Override
    public List<TbContent> getBannerList() {
        ValueOperations<String, String> str = stringRedisTemplate.opsForValue();
        String banner = str.get(RedisKey.BANNER);
        if (banner != null){
            //存在
            return new Gson().fromJson(
                    banner,new TypeToken<List<Map<String, Object>>>(){}.getType());
        }else {
            //不存在
            List<TbContent> bannerList = contentMapper.getBannerList();
            String map = new Gson().toJson(bannerList);
            str.set(RedisKey.BANNER,map);
            return bannerList;
        }
    }

    //获取 每日推荐
    @Override
    public List<TbContent> getDayPushList() {
        ValueOperations<String, String> str = stringRedisTemplate.opsForValue();
        String dayPush = str.get(RedisKey.DAYPUSH);
        if (dayPush != null){
            //存在
            return new Gson().fromJson(
                    dayPush,new TypeToken<List<Map<String, Object>>>(){}.getType());
        }else {
            //不存在
            List<TbContent> dayPushList = contentMapper.getDayPushList();
            String map = new Gson().toJson(dayPushList);
            str.set(RedisKey.DAYPUSH,map);
            return dayPushList;
        }
    }

    //获取猜你喜欢广告
    @Override
    public List<TbContent> getYouLikeList() {
        SetOperations<String, String> set = stringRedisTemplate.opsForSet();
        if (stringRedisTemplate.hasKey(RedisKey.YOULIKE)){
            //存在
            List<String> list = set.randomMembers(RedisKey.YOULIKE, 6L);
            List<TbContent> contentList = new ArrayList<>();
            for (String s : list) {
                TbContent content = JSON.parseObject(s, TbContent.class);
                contentList.add(content);
            }
            return contentList;

        }else {
            List<TbContent> youLikeList = contentMapper.getYouLikeList();
            for (TbContent content : youLikeList) {
                set.add(RedisKey.YOULIKE,JSON.toJSONString(content));
            }
            return youLikeList;
        }

    }

}

